package common.util;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


import com.zdxc.entities.Userinfo;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.beanutils.converters.StringConverter;

//import sysmgt.vo.UserVO;

import common.constant.GlobalConstant;

/**
 * JavaBean工具类
 * 
 * @author liuxj
 * @version 1.0.0 2015年2月28日
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public class JavaBeanUtils {
    /** 空时间 */
    private static final String EMPTY_TIME = "00:00:00";

    static {
        // 添加Date to String的转换器
        ConvertUtils.register(new Converter() {
            @Override
            public Object convert(Class type, Object value) {
                if (value != null && value instanceof Date) {
                    String time = DateUtils.dateToStr((Date) value, GlobalConstant.TIME_PATTERN);
                    if (time.equals(EMPTY_TIME)) {
                        return DateUtils.dateToStr((Date) value, GlobalConstant.DATE_PATTERN);
                    } else {
                        return DateUtils.dateToStr((Date) value, GlobalConstant.DATE_TIME_PATTERN);
                    }
                }

                return new StringConverter().convert(type, value);
            }
        }, String.class);

        // 添加String to Date的转换器
        ConvertUtils.register(new Converter() {
            @Override
            public Object convert(Class type, Object value) {
                if (value == null) {
                    return null;
                }

                if (value instanceof Date) {
                    return value;
                }

                String date = (String) value;
                if (date.length() > 10) {
                    return DateUtils.strToDate(date, GlobalConstant.DATE_TIME_PATTERN);
                } else {
                    return DateUtils.strToDate(date, GlobalConstant.DATE_PATTERN);
                }
            }
        }, Date.class);
    }

    /**
     * 将JavaBean转换成Map
     * 
     * @param bean
     * @return Map<String, String>
     */
    public static Map<String, String> beanToStringMap(Object bean) {
        Map<String, String> map = new HashMap<String, String>();
        try {
            map = BeanUtils.describe(bean);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return map;
    }

    /**
     * 将JavaBean转换成Map
     * 
     * @param bean
     * @return Map<String, Object>
     */
    public static Map<String, Object> beanToObjectMap(Object bean) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            map = PropertyUtils.describe(bean);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return map;
    }

    /**
     * 将Map转换成JavaBean
     * 
     * @param map
     * @param bean
     */
    public static void mapToBean(Map<String, ? extends Object> map, Object bean) {
        try {
            BeanUtils.populate(bean, map);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String date = DateUtils.dateToStr(new Date(), GlobalConstant.DATE_TIME_PATTERN);
        Date today = DateUtils.strToDate(date, GlobalConstant.DATE_TIME_PATTERN);

        Userinfo user = new Userinfo();
        user.setUserName("admin");
        user.setPasswd("123456");
      //  user.setCreationDate(today);

        Map<String, String> map = beanToStringMap(user);
     // Map<String, Object> map = beanToObjectMap(user);
        ////System.out.println(map);

        user = new Userinfo();
        mapToBean(map, user);

        ////System.out.println(user);
    }
}
